<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>JVM | Mr Wang</title><meta name="keywords" content="Java"><meta name="author" content="Mr Wang"><meta name="copyright" content="Mr Wang"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="一、什么是JVM定义JVM是Java程序的运行环境 优点 一次编译到处运行 自动内存管理，垃圾回收机制 数组下标越界检查  比较JVM、JRE、JDK 关系图  二、内存模型整体模型 1、程序计数器1.1 作用是记住下一条 jvm指令的执行地址 1.2 特点 线程私有 CPU会为每个线程分配时间片，当当前线程的时间片使用完以后，CPU就会去执行另一个线程中的代码 程序计数器是每个线程所私有的，当另">
<meta property="og:type" content="article">
<meta property="og:title" content="JVM">
<meta property="og:url" content="https://menu88.gitee.io/blogs/2021/06/09/JVM/index.html">
<meta property="og:site_name" content="Mr Wang">
<meta property="og:description" content="一、什么是JVM定义JVM是Java程序的运行环境 优点 一次编译到处运行 自动内存管理，垃圾回收机制 数组下标越界检查  比较JVM、JRE、JDK 关系图  二、内存模型整体模型 1、程序计数器1.1 作用是记住下一条 jvm指令的执行地址 1.2 特点 线程私有 CPU会为每个线程分配时间片，当当前线程的时间片使用完以后，CPU就会去执行另一个线程中的代码 程序计数器是每个线程所私有的，当另">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://deecyn.com/img/jvm/JVM%E5%86%85%E5%AD%98%E5%8C%BA%E5%9F%9F%E5%8F%8A%E7%BB%93%E6%9E%84%E5%9B%BE.jpg">
<meta property="article:published_time" content="2021-06-09T00:59:29.332Z">
<meta property="article:modified_time" content="2021-06-09T08:29:58.193Z">
<meta property="article:author" content="Mr Wang">
<meta property="article:tag" content="Java">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://deecyn.com/img/jvm/JVM%E5%86%85%E5%AD%98%E5%8C%BA%E5%9F%9F%E5%8F%8A%E7%BB%93%E6%9E%84%E5%9B%BE.jpg"><link rel="shortcut icon" href="/blogs/img/favicon.jpg"><link rel="canonical" href="https://menu88.gitee.io/blogs/2021/06/09/JVM/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blogs/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blogs/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'JVM',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-06-09 16:29:58'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><meta name="generator" content="Hexo 5.4.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/blogs/img/favicon.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/blogs/archives/"><div class="headline">文章</div><div class="length-num">8</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blogs/tags/"><div class="headline">标签</div><div class="length-num">1</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blogs/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/Linux"><i class="fa-fw fas fa-Linux"></i><span> Linux</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/photo"><i class="fa-fw fas fa-photo"></i><span> Photo</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> List</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blogs/music/"><i class="fa-fw fas fa-music"></i><span> Music</span></a></li><li><a class="site-page child" href="/blogs/movies/"><i class="fa-fw fas fa-video"></i><span> Movie</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/blogs/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://deecyn.com/img/jvm/JVM%E5%86%85%E5%AD%98%E5%8C%BA%E5%9F%9F%E5%8F%8A%E7%BB%93%E6%9E%84%E5%9B%BE.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blogs/">Mr Wang</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blogs/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/Linux"><i class="fa-fw fas fa-Linux"></i><span> Linux</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/photo"><i class="fa-fw fas fa-photo"></i><span> Photo</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> List</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blogs/music/"><i class="fa-fw fas fa-music"></i><span> Music</span></a></li><li><a class="site-page child" href="/blogs/movies/"><i class="fa-fw fas fa-video"></i><span> Movie</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/blogs/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">JVM</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-06-09T00:59:29.332Z" title="发表于 2021-06-09 08:59:29">2021-06-09</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-06-09T08:29:58.193Z" title="更新于 2021-06-09 16:29:58">2021-06-09</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="JVM"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="一、什么是JVM"><a href="#一、什么是JVM" class="headerlink" title="一、什么是JVM"></a>一、什么是JVM</h2><h3 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h3><p>JVM是Java程序的运行环境</p>
<h3 id="优点"><a href="#优点" class="headerlink" title="优点"></a>优点</h3><ul>
<li>一次编译到处运行</li>
<li>自动内存管理，垃圾回收机制</li>
<li>数组下标越界检查</li>
</ul>
<h3 id="比较"><a href="#比较" class="headerlink" title="比较"></a>比较</h3><p>JVM、JRE、JDK 关系图</p>
<p><img src="https://nyimapicture.oss-cn-beijing.aliyuncs.com/img/20200608150422.png"></p>
<h2 id="二、内存模型"><a href="#二、内存模型" class="headerlink" title="二、内存模型"></a>二、内存模型</h2><h3 id="整体模型"><a href="#整体模型" class="headerlink" title="整体模型"></a>整体模型</h3><p><img src="https://nyimapicture.oss-cn-beijing.aliyuncs.com/img/20200608150440.png"></p>
<h3 id="1、程序计数器"><a href="#1、程序计数器" class="headerlink" title="1、程序计数器"></a>1、程序计数器</h3><h4 id="1-1-作用"><a href="#1-1-作用" class="headerlink" title="1.1 作用"></a>1.1 作用</h4><p>是记住下一条 jvm指令的执行地址</p>
<h4 id="1-2-特点"><a href="#1-2-特点" class="headerlink" title="1.2 特点"></a>1.2 特点</h4><ul>
<li>线程私有<ul>
<li>CPU会为每个线程分配时间片，当当前线程的时间片使用完以后，CPU就会去执行另一个线程中的代码</li>
<li>程序计数器是<strong>每个线程</strong>所<strong>私有</strong>的，当另一个线程的时间片用完，又返回来执行当前线程的代码时，通过程序计数器可以知道应该执行哪一句指令</li>
</ul>
</li>
<li>不会存在内存溢出（本身设计）</li>
</ul>
<h3 id="2、虚拟机栈"><a href="#2、虚拟机栈" class="headerlink" title="2、虚拟机栈"></a>2、虚拟机栈</h3><h4 id="2-1-定义"><a href="#2-1-定义" class="headerlink" title="2.1 定义"></a>2.1 定义</h4><ul>
<li>每个线程运行时所需要的内存称为虚拟机栈</li>
<li>每个栈由多个栈帧组成，对应着每次方法调用时所占的内存</li>
<li>每个线程只能也只有一个栈帧，对应着正在执行的那个方法</li>
</ul>
<p><img src="JVM/zz1.jpg"></p>
<h4 id="2-2-问题辨析"><a href="#2-2-问题辨析" class="headerlink" title="2.2 问题辨析"></a>2.2 问题辨析</h4><p>1.垃圾回收是否涉及栈内存</p>
<ul>
<li><strong>不需要</strong>。因为虚拟机栈中是由一个个栈帧组成的，在方法执行完毕后，对应的栈帧就会被弹出栈。所以无需通过垃圾回收机制去回收内存。</li>
</ul>
<p>2.栈内存的分配越大越好吗？</p>
<ul>
<li>不是。因为<strong>物理内存是一定的</strong>，栈内存越大，可以支持更多的递归调用，但是可执行的线程数就会越少。</li>
</ul>
<p>3.方法内的局部变量是否是线程安全的？</p>
<ul>
<li>如果方法内<strong>局部变量没有逃离方法的作用范围</strong>，则是<strong>线程安全</strong>的</li>
<li>如果如果<strong>局部变量引用了对象</strong>，并<strong>逃离了方法的作用范围</strong>，则需要考虑线程安全问题</li>
</ul>
<h4 id="2-3-栈内存溢出"><a href="#2-3-栈内存溢出" class="headerlink" title="2.3 栈内存溢出"></a>2.3 栈内存溢出</h4><p>  <strong>Java.lang.stackOverflowError</strong> 栈内存溢出</p>
<p><strong>发生原因</strong></p>
<ul>
<li>栈中栈帧<strong>过多</strong>（无线递归）</li>
<li>每个栈帧内存占用<strong>过大</strong>（概率很小）</li>
</ul>
<h4 id="2-4-线程运行诊断"><a href="#2-4-线程运行诊断" class="headerlink" title="2.4 线程运行诊断"></a>2.4 线程运行诊断</h4><p><strong>CPU占用过高</strong></p>
<ul>
<li>Linux环境下运行某些程序的时候，可能导致CPU的占用过高，这时需要定位占用CPU过高的线程<ul>
<li><strong>top</strong>命令，查看是哪个<strong>进程</strong>占用CPU过高</li>
<li><strong>ps H -eo pid, tid（线程id）, %cpu | grep 刚才通过top查到的进程号</strong> 通过ps命令进一步查看是哪个线程占用CPU过高</li>
<li><strong>jstack 进程id</strong> 通过查看进程中的线程的nid，刚才通过ps命令看到的tid来<strong>对比定位</strong>，注意jstack查找出的线程id是<strong>16进制的</strong>，<strong>需要转换</strong></li>
</ul>
</li>
</ul>
<p><strong>程序运行长时间没有响应结果</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">jps -l // 查看java进程</span><br><span class="line">jstack 进程号 // 查看堆栈信息</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">HoldLockDemo</span> <span class="keyword">implements</span> <span class="title">Runnable</span></span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String LockA;</span><br><span class="line">    <span class="keyword">private</span> String LockB;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">HoldLockDemo</span><span class="params">(String lockA,String lockB)</span></span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.LockA=lockA;</span><br><span class="line">        <span class="keyword">this</span>.LockB=lockB;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">synchronized</span> (LockA)&#123;</span><br><span class="line">            System.out.println(Thread.currentThread().getName()+<span class="string">&quot;持有锁&quot;</span>+LockA+<span class="string">&quot;等待获取&quot;</span>+LockB);</span><br><span class="line">            <span class="keyword">synchronized</span> (LockB)&#123;</span><br><span class="line">                System.out.println(Thread.currentThread().getName()+<span class="string">&quot;持有锁&quot;</span>+LockB+<span class="string">&quot;等待获取&quot;</span>+LockA);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Tests</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">new</span> Thread(<span class="keyword">new</span> HoldLockDemo(<span class="string">&quot;1&quot;</span>,<span class="string">&quot;2&quot;</span>),<span class="string">&quot;A&quot;</span>).start();</span><br><span class="line">        <span class="keyword">new</span> Thread(<span class="keyword">new</span> HoldLockDemo(<span class="string">&quot;2&quot;</span>,<span class="string">&quot;1&quot;</span>),<span class="string">&quot;B&quot;</span>).start();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>上面程序会出现死锁（怎么排除）</p>
<p><img src="JVM/jvm2.jpg"></p>
<p>jsp -l</p>
<p><img src="JVM/jvm3.jpg"></p>
<p>jstack 15816</p>
<p><img src="JVM/jvm4.jpg"></p>
<p><img src="JVM/jvm5.jpg"></p>
<p>根据上图可以看出造成程序不继续执行，长时间等待的原因为死锁</p>
<h3 id="3-本地方法栈"><a href="#3-本地方法栈" class="headerlink" title="3.本地方法栈"></a>3.本地方法栈</h3><p><img src="https://nyimapicture.oss-cn-beijing.aliyuncs.com/img/20200608150440.png"></p>
<h4 id="3-1-作用"><a href="#3-1-作用" class="headerlink" title="3.1 作用"></a>3.1 作用</h4><p>给本地方法运行提供一个运行空间</p>
<p>不是由Java代码编写的，而是由其他的语言编写的直接与计算机打交道</p>
<p>一些带有<strong>native关键字</strong>的方法就是需要JAVA去调用本地的C或者C++方法，因为JAVA有时候没法直接和操作系统底层交互，所以需要用到本地方法</p>
<h3 id="4-堆"><a href="#4-堆" class="headerlink" title="4.堆"></a>4.堆</h3><h4 id="4-1-定义"><a href="#4-1-定义" class="headerlink" title="4.1 定义"></a>4.1 定义</h4><p>通过new关键字<strong>创建的对象</strong>都会被放在堆内存</p>
<h4 id="4-2-特点"><a href="#4-2-特点" class="headerlink" title="4.2 特点"></a>4.2 特点</h4><ul>
<li>是线程共享的，堆中的对象都要考虑线程安全问题（<strong>有例外</strong>）</li>
<li>有垃圾回收机制</li>
</ul>
<h4 id="4-3-堆内存溢出"><a href="#4-3-堆内存溢出" class="headerlink" title="4.3 堆内存溢出"></a>4.3 堆内存溢出</h4><p><strong>java.lang.OutofMemoryError</strong> ：java heap space. 堆内存溢出</p>
<p><strong>内存溢出代码演示</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">       <span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line">       <span class="keyword">try</span> &#123;</span><br><span class="line">           List&lt;String&gt; list = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">           String str = <span class="string">&quot;i&quot;</span>;</span><br><span class="line">           System.out.println(i);</span><br><span class="line">           <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">               list.add(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">               i++;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line"></span><br><span class="line">           System.out.println(<span class="string">&quot;&gt;&gt;&quot;</span>+i);</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<p>堆内存溢出错误如下：</p>
<p><img src="JVM/jvm6.jpg"></p>
<h4 id="4-4-堆内存诊断工具"><a href="#4-4-堆内存诊断工具" class="headerlink" title="4.4 堆内存诊断工具"></a>4.4 堆内存诊断工具</h4><p><strong>1.jps</strong></p>
<ul>
<li>查看当前系统有哪些Java进程</li>
</ul>
<p><strong>2.jmap</strong></p>
<ul>
<li>查看堆内存占用情况 jmap -heap 进程id</li>
</ul>
<p><strong>3.jconsole</strong></p>
<ul>
<li>图形界面，多功能检测工具</li>
</ul>
<p><img src="JVM/jvm7.jpg"></p>
<p><strong>4.jvisualvm</strong></p>
<ul>
<li>图形化工具，比jconsole更加强大</li>
</ul>
<h3 id="5-方法区"><a href="#5-方法区" class="headerlink" title="5.方法区"></a>5.方法区</h3><h4 id="5-1-定义"><a href="#5-1-定义" class="headerlink" title="5.1 定义"></a>5.1 定义</h4></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Mr Wang</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://menu88.gitee.io/blogs/2021/06/09/JVM/">https://menu88.gitee.io/blogs/2021/06/09/JVM/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://menu88.gitee.io/blogs" target="_blank">Mr Wang</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blogs/tags/Java/">Java</a></div><div class="post_share"><div class="social-share" data-image="https://deecyn.com/img/jvm/JVM%E5%86%85%E5%AD%98%E5%8C%BA%E5%9F%9F%E5%8F%8A%E7%BB%93%E6%9E%84%E5%9B%BE.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blogs/2021/06/10/Spring/"><img class="prev-cover" src="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=650243093,1957377604&amp;fm=26&amp;gp=0.jpg" onerror="onerror=null;src='/blogs/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Spring</div></div></a></div><div class="next-post pull-right"><a href="/blogs/2021/06/08/JUC/"><img class="next-cover" src="https://images2017.cnblogs.com/blog/1222878/201711/1222878-20171111160819825-27801316.png" onerror="onerror=null;src='/blogs/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">JUC</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/blogs/2021/06/08/JUC/" title="JUC"><img class="cover" src="https://images2017.cnblogs.com/blog/1222878/201711/1222878-20171111160819825-27801316.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-06-08</div><div class="title">JUC</div></div></a></div><div><a href="/blogs/2021/06/11/Redis/" title="Redis"><img class="cover" src="https://www.runoob.com/wp-content/uploads/2014/11/redis.gif" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-06-11</div><div class="title">Redis</div></div></a></div><div><a href="/blogs/2021/06/10/Spring/" title="Spring"><img class="cover" src="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=650243093,1957377604&fm=26&gp=0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-06-10</div><div class="title">Spring</div></div></a></div><div><a href="/blogs/2021/06/08/interview/" title="汇总"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F13516282245%2F1000&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625905439&t=9fd3b6120db5aa6b896dc28541be0144" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-06-08</div><div class="title">汇总</div></div></a></div><div><a href="/blogs/2021/06/10/SpringBoot/" title="Spring Boot"><img class="cover" src="https://upload.jianshu.io/collections/images/230545/images.png?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-06-10</div><div class="title">Spring Boot</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="/blogs/img/favicon.jpg" onerror="this.onerror=null;this.src='/blogs/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">Mr Wang</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/blogs/archives/"><div class="headline">文章</div><div class="length-num">8</div></a></div><div class="card-info-data-item is-center"><a href="/blogs/tags/"><div class="headline">标签</div><div class="length-num">1</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E4%BB%80%E4%B9%88%E6%98%AFJVM"><span class="toc-number">1.</span> <span class="toc-text">一、什么是JVM</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89"><span class="toc-number">1.1.</span> <span class="toc-text">定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BC%98%E7%82%B9"><span class="toc-number">1.2.</span> <span class="toc-text">优点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%AF%94%E8%BE%83"><span class="toc-number">1.3.</span> <span class="toc-text">比较</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%86%85%E5%AD%98%E6%A8%A1%E5%9E%8B"><span class="toc-number">2.</span> <span class="toc-text">二、内存模型</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B4%E4%BD%93%E6%A8%A1%E5%9E%8B"><span class="toc-number">2.1.</span> <span class="toc-text">整体模型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E7%A8%8B%E5%BA%8F%E8%AE%A1%E6%95%B0%E5%99%A8"><span class="toc-number">2.2.</span> <span class="toc-text">1、程序计数器</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-1-%E4%BD%9C%E7%94%A8"><span class="toc-number">2.2.1.</span> <span class="toc-text">1.1 作用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-2-%E7%89%B9%E7%82%B9"><span class="toc-number">2.2.2.</span> <span class="toc-text">1.2 特点</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81%E8%99%9A%E6%8B%9F%E6%9C%BA%E6%A0%88"><span class="toc-number">2.3.</span> <span class="toc-text">2、虚拟机栈</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#2-1-%E5%AE%9A%E4%B9%89"><span class="toc-number">2.3.1.</span> <span class="toc-text">2.1 定义</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-2-%E9%97%AE%E9%A2%98%E8%BE%A8%E6%9E%90"><span class="toc-number">2.3.2.</span> <span class="toc-text">2.2 问题辨析</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-3-%E6%A0%88%E5%86%85%E5%AD%98%E6%BA%A2%E5%87%BA"><span class="toc-number">2.3.3.</span> <span class="toc-text">2.3 栈内存溢出</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-4-%E7%BA%BF%E7%A8%8B%E8%BF%90%E8%A1%8C%E8%AF%8A%E6%96%AD"><span class="toc-number">2.3.4.</span> <span class="toc-text">2.4 线程运行诊断</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E6%9C%AC%E5%9C%B0%E6%96%B9%E6%B3%95%E6%A0%88"><span class="toc-number">2.4.</span> <span class="toc-text">3.本地方法栈</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-%E4%BD%9C%E7%94%A8"><span class="toc-number">2.4.1.</span> <span class="toc-text">3.1 作用</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E5%A0%86"><span class="toc-number">2.5.</span> <span class="toc-text">4.堆</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-1-%E5%AE%9A%E4%B9%89"><span class="toc-number">2.5.1.</span> <span class="toc-text">4.1 定义</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-%E7%89%B9%E7%82%B9"><span class="toc-number">2.5.2.</span> <span class="toc-text">4.2 特点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-%E5%A0%86%E5%86%85%E5%AD%98%E6%BA%A2%E5%87%BA"><span class="toc-number">2.5.3.</span> <span class="toc-text">4.3 堆内存溢出</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-4-%E5%A0%86%E5%86%85%E5%AD%98%E8%AF%8A%E6%96%AD%E5%B7%A5%E5%85%B7"><span class="toc-number">2.5.4.</span> <span class="toc-text">4.4 堆内存诊断工具</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-%E6%96%B9%E6%B3%95%E5%8C%BA"><span class="toc-number">2.6.</span> <span class="toc-text">5.方法区</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#5-1-%E5%AE%9A%E4%B9%89"><span class="toc-number">2.6.1.</span> <span class="toc-text">5.1 定义</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blogs/2021/07/14/shell/" title="无题"><img src="https://cdn.jsdelivr.net/gh/jerryc127/CDN@latest/cover/default_bg3.png" onerror="this.onerror=null;this.src='/blogs/img/404.jpg'" alt="无题"/></a><div class="content"><a class="title" href="/blogs/2021/07/14/shell/" title="无题">无题</a><time datetime="2021-07-14T07:01:36.574Z" title="发表于 2021-07-14 15:01:36">2021-07-14</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blogs/2021/07/06/Linux/" title="无题"><img src="https://cdn.jsdelivr.net/gh/jerryc127/CDN@latest/cover/default_bg.png" onerror="this.onerror=null;this.src='/blogs/img/404.jpg'" alt="无题"/></a><div class="content"><a class="title" href="/blogs/2021/07/06/Linux/" title="无题">无题</a><time datetime="2021-07-06T09:16:15.029Z" title="发表于 2021-07-06 17:16:15">2021-07-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blogs/2021/06/11/Redis/" title="Redis"><img src="https://www.runoob.com/wp-content/uploads/2014/11/redis.gif" onerror="this.onerror=null;this.src='/blogs/img/404.jpg'" alt="Redis"/></a><div class="content"><a class="title" href="/blogs/2021/06/11/Redis/" title="Redis">Redis</a><time datetime="2021-06-11T02:45:24.000Z" title="发表于 2021-06-11 10:45:24">2021-06-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blogs/2021/06/10/SpringBoot/" title="Spring Boot"><img src="https://upload.jianshu.io/collections/images/230545/images.png?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240" onerror="this.onerror=null;this.src='/blogs/img/404.jpg'" alt="Spring Boot"/></a><div class="content"><a class="title" href="/blogs/2021/06/10/SpringBoot/" title="Spring Boot">Spring Boot</a><time datetime="2021-06-10T08:04:41.217Z" title="发表于 2021-06-10 16:04:41">2021-06-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blogs/2021/06/10/Spring/" title="Spring"><img src="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=650243093,1957377604&amp;fm=26&amp;gp=0.jpg" onerror="this.onerror=null;this.src='/blogs/img/404.jpg'" alt="Spring"/></a><div class="content"><a class="title" href="/blogs/2021/06/10/Spring/" title="Spring">Spring</a><time datetime="2021-06-10T01:50:43.000Z" title="发表于 2021-06-10 09:50:43">2021-06-10</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By Mr Wang</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blogs/js/utils.js"></script><script src="/blogs/js/main.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>